/**
 * 载入项目引导文件
 */
require('./bootstrap');

//初始化树结构
let initTree = (group_id = 0) => {
    let tree_data = {};
    utils.ajaxGet(page_params.url_list, {}, (response, data) =>{
        if (response.data.treeList){
            tree_data = response.data.treeList;
            $('#tree').treeview({data: tree_data,levels:2,showTags:!0,collapseIcon:'fa fa-chevron-down',expandIcon:'fa fa-chevron-right'});
            //绑定选中事件
            $('#tree').on('nodeSelected', function(event, data) {
                asxBox = layer.open({
                    type: 1,
                    title: '菜单操作',
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    skin: 'layui-layer-molv', //加上边框
                    area: ['520px', '150px'], //宽高
                    content: $('#box_ask')
                });
                nowNode = data;
            });
        }
    });
};

let box;
let asxBox;
let nowNode;

//不同菜单类别切换输入框
let typechange = (type)=> {
    $('.hidebox').hide();
    if ($.inArray(type, ['click','scancode_waitmsg','scancode_push','pic_sysphoto','pic_photo_or_album','pic_weixin','location_select']) != -1){
        $('.keybox').show();
    }
    if ($.inArray(type, ['media_id','view_limited']) != -1){
        $('.mediaidbox').show();
    }
    if ($.inArray(type, ['view','miniprogram']) != -1){
        $('.urlbox').show();
    }
    if ($.inArray(type, ['miniprogram']) != -1){
        $('.appidbox,.pagepathbox').show();
    }
}
//个性化设置
let conditionalchange = (isconditional) => {
    if (isconditional == 1){
        $('.rulebox').show();
    }else{
        $('.rulebox').hide();
    }
}
//构建地区选择元素
let buildAreaHtml = (domId, parentId, defaultVal) => {
    let html = '<option value="0">请选择</option>';
    if (page_params.data_area[parentId]) {
        _.each(page_params.data_area[parentId], function (n, k) {
            html += '<option  value="' +n.id+ '" '+ (n.id== defaultVal ? 'selected' : '') +'>'+ n.name +'</option>';
        });
    }
    $('#'+domId).html(html);
}


$(document).ready(()=>{
    //切换树结构
    initTree();
    //菜单类别切换
    typechange($("#type").val());
    $('#type').on('change', function () {
        typechange($(this).val());
    });
    //个性化设置切换
    conditionalchange($("input[name='is_conditional']:checked").val());
    $("input[name='is_conditional']").change(function () {
        conditionalchange($(this).val());
    });
    //初始化地区
    buildAreaHtml('country', 0);
    buildAreaHtml('province', -1);
    buildAreaHtml('city', -1);
    //地区动态切换
    $('#country').change(function () {
        buildAreaHtml('province', $(this).val());
        buildAreaHtml('city', -1);
    });
    $('#province').change(function () {
        buildAreaHtml('city', $(this).val());
    });

    //添加顶级菜单
    $("#btn-add").on('click', () => {
        $("#box-form form").resetForm();
        $("#box-form form").attr('action', page_params.url_add);
        $("#box-form input[name='menu_id']").val(0);
        $("#box-form input[name='parent_id']").val(0);
        typechange($("#type").val());
        conditionalchange($("input[name='is_conditional']:checked").val());
        buildAreaHtml('country', 0);
        buildAreaHtml('province', -1);
        buildAreaHtml('city', -1);
        box = layer.open({
            type: 1,
            title: '添加【顶级菜单】',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '500px'], //宽高
            content: $('#box-form')
        });
    });
    //添加子菜单
    $("#btn-add-child").on('click', function () {
        $("#box-form form").resetForm();
        $("#box-form form").attr('action', page_params.url_add);
        $("#box-form input[name='menu_id']").val(0);
        $("#box-form input[name='parent_id']").val(nowNode.menu_id);
        typechange($("#type").val());
        conditionalchange($("input[name='is_conditional']:checked").val());
        buildAreaHtml('country', 0);
        buildAreaHtml('province', -1);
        buildAreaHtml('city', -1);
        box = layer.open({
            type: 1,
            title: '添加【' + nowNode.display_name + '】子角色',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '500px'], //宽高
            content: $('#box-form')
        });
    });
    //编辑菜单
    $("#btn-edit").on('click', function () {
        $("#box-form form").attr('action', page_params.url_edit);
        $("#box-form form").resetForm();

        $("#box-form input[name='menu_id']").val(nowNode.menu_id);
        $("#box-form input[name='parent_id']").val(nowNode.parent_id);
        $("#box-form input[name='name']").val(nowNode.name);
        $("#box-form select[name='type']").val(nowNode.type);
        typechange(nowNode.type);
        $("#box-form input[name='key']").val(nowNode.key);
        $("#box-form input[name='url']").val(nowNode.url);
        $("#box-form input[name='appid']").val(nowNode.appid);
        $("#box-form input[name='pagepath']").val(nowNode.pagepath);
        $("#box-form input[name='media_id']").val(nowNode.media_id);
        $("#box-form input[name='order_num']").val(nowNode.order_num);
        //个性化设置
        $(":radio[name='is_conditional'][value='" + nowNode.is_conditional + "']").prop("checked", "checked");
        conditionalchange(nowNode.is_conditional);
        //标签
        if (nowNode.matchrule.tag_id){
            $("#tag_id").val(nowNode.matchrule.tag_id);
        }
        //性别
        if (nowNode.matchrule.sex){
            $("#sex").val(nowNode.matchrule.sex);
        }
        //客户端版本
        if (nowNode.matchrule.client_platform_type){
            $("#client_platform_type").val(nowNode.matchrule.client_platform_type);
        }
        //地区
        if (nowNode.matchrule.countryId){
            buildAreaHtml('country', 0, nowNode.matchrule.countryId);
        }else{
            buildAreaHtml('country', 0);
        }
        if (nowNode.matchrule.countryId && nowNode.matchrule.provinceId){
            buildAreaHtml('province', nowNode.matchrule.countryId, nowNode.matchrule.provinceId);
        }else if(nowNode.matchrule.countryId){
            buildAreaHtml('province', nowNode.matchrule.countryId);
        }else{
            buildAreaHtml('province', -1);
        }
        if (nowNode.matchrule.provinceId && nowNode.matchrule.cityId){
            buildAreaHtml('city', nowNode.matchrule.provinceId, nowNode.matchrule.cityId);
        }else if(nowNode.matchrule.provinceId){
            buildAreaHtml('city', nowNode.matchrule.provinceId);
        }else{
            buildAreaHtml('city', -1);
        }
        //语言
        if (nowNode.matchrule.language){
            $("#language").val(nowNode.matchrule.language);
        }
        box = layer.open({
            type: 1,
            title: '编辑菜单',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '500px'], //宽高
            content: $('#box-form')
        });
    });
    //提交表单
    $('#box-form .btn-sub').on('click', function () {
        data = {};
        data.countryId = $("#country").val();
        data.country = $("#country").find("option:selected").text();
        data.provinceId = $("#province").val();
        data.province = $("#province").find("option:selected").text();
        data.cityId = $("#city").val();
        data.city = $("#city").find("option:selected").text();

        utils.ajaxSubmitForm($("#box-form form"), '', data, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                layer.close(asxBox);
                initTree();
            }
        });
    });
    //删除
    $("#btn-del").on('click', function () {
        layer.close(box);
        if(nowNode.nodes && nowNode.nodes.length > 0 || nowNode._nodes && nowNode._nodes.length > 0){
            layer.alert('该菜单下有子菜单，不可删除', {icon:2, title:'失败提示', closeBtn:0, anim:6});
            return false;
        }
        let menu_id = 0;
        if (nowNode) {
            menu_id = nowNode.menu_id;
        }
        utils.confirm(page_params.url_del, {'id': menu_id}, function (response, postdata) {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(asxBox);
                initTree();
            }
        },'确定要删除菜单【' + nowNode.name + '】？');
    });
    //发布推送菜单
    $("#btn-publish").on('click', function () {
        utils.confirm(page_params.url_publish, {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
        },'你确定发布推送菜单么？', '该推送会更新现有服务号的菜单配置');
    });
    //同步菜单数据
    $("#btn-sync").on('click', function () {
        utils.confirm(page_params.url_sync, {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                initTree();
            }
        },'你确定同步菜单么？', '该同步过程可能花费大量时间');
    });
});